<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Area;
use App\Film_type;
use App\Film_att;
use App\Film;

class MovController extends Controller
{
    public function movadd(Request $req)
    {

        if (empty($_POST)) {
           $area=DB::table('areas')->get();
           $film_type=DB::table('film_types')->get();
          return view('Admin/movieadd',['area'=>$area,'film_type'=>$film_type]);

        }

        $film_att= new \App\Film_att();
        $film=new \App\Film();

        $a='./Uploads/'.date("Y-m-d");
        $b=rand(100,999).'.jpg';
        $c=rand(100,999).'.jpg';
        //dd($b);
        $req->file('org_img')->move($a,$b);
        $req->file('movie')->move($a,$c);
        $film->title = $req->title;
        $film->actors = $req->actors;
        $film->link = $req->link;
        $film->intro = $req->intro;
        $film->org_img=$a."/".$b;
        $film->movie=$a."/".$c;
        $film->type_id =$req->type_id;
        $film->area_id=$req->area_id;
        $film->save();


        $film_att->fid=$film->fid;
        $film_att->is_pay = $req->is_pay;
        $film_att->is_new = $req->is_new;
        $film_att->is_rec = $req->is_rec;
        $film_att->is_hot = $req->is_hot;
        $film_att->period = $req->period;
        $film_att->pubtime=time();
        $film_att->save();



         return  redirect('Admin/mv');

    }

    public function movlist(){
        $row=DB::table('films')
            ->leftjoin('film_atts','films.fid','=','film_atts.fid')
            ->leftjoin('areas','films.area_id','=','areas.area_id')
            ->leftjoin('film_types','film_types.type_id','=','films.type_id')
            ->get();
        return view('Admin/movlist',['row'=>$row]);
    }

    public function del($fid){
        $f=Film::find($fid);
        $ur=$f['org_img'];
        $uv=$f['movie'];

       if(Film::find($fid)->delete()&&Film_att::where('fid',$fid)->delete()){
        unlink($ur);
        unlink($uv);
           return redirect('Admin/mvlist');
        }else{
            return '删除失败';
       }
    }

    public function edit(Request $req,$fid)
    {
        if (!$_POST) {
            $film=DB::table('films')->where('fid','=',$fid)->first();
            $film_att =DB::table('film_atts')->where('aid','=',$fid)->first();
            $area=DB::table('areas')->get();
            $film_type=DB::table('film_types')->get();
            return view('Admin/movieedit', ['film' => $film,'film_att'=>$film_att,'area'=>$area,'film_type'=>$film_type]);
        } else {
            $film = Film::find($fid);
            $film_att = Film_att::find($fid);

            $film->title=$_POST['title'];
            $film->actors=$_POST['actors'];
            $film->link=$_POST['link'];
            $film->intro=$_POST['intro'];
            $film->area_id=$_POST['area_id'];
            $film->type_id=$_POST['type_id'];
            $film->save();

            $film_att->is_pay=$_POST['is_pay'];
            $film_att->is_hot=$_POST['is_hot'];
            $film_att->is_rec=$_POST['is_rec'];
            $film_att->is_new=$_POST['is_new'];
            $film_att->save();

            return back()->withInput();





        }


    }



}





